/*
 *  atBudget Personal Finance Management application
 *
 *  Copyright 2011, Ilia Frenkel <frenkel.ilia@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
	Currency model defines fields of the currency and proxy for server
	communication.
 */
Ext.define('ATB.model.Currency', {
    extend: 'Ext.data.Model',
    fields: [
		{name: 'id',        type: 'int'                   },
		{name: 'name',      type: 'string'                },
		{name: 'code',      type: 'string'                },
		{name: 'prefix',    type: 'string'                },
		{name: 'suffix',    type: 'string'                },
		{name: 'thousands', type: 'string'                },
		{name: 'decimal',   type: 'string'                },
		{name: 'precision', type: 'int',   defaultValue: 2},
		{name: 'codename',  type: 'string', persist: false,
			convert: function(value, record) {
				return record.get('code') + ' | ' + record.get('name');
			}
		},
		{name: 'examplefmt',type: 'string', persist: false,
			convert: function(value, record) {
				var suffix = record.get('suffix') ? record.get('suffix') : '';
				Ext.util.Format.currencyPrecision  = record.get('precision');
				Ext.util.Format.currencySign       = 
					record.get('prefix') ? record.get('prefix') : '';
				Ext.util.Format.decimalSeparator   = record.get('decimal');
				Ext.util.Format.thousandSeparator  = record.get('thousands');
				return Ext.util.Format.currency('12345.67') + suffix;
			}
		}
	],//fields
	proxy: {
		type: 'rest',
		url: 'data/currencies/index.php',
		reader: {
			type: 'json',
			root: 'currencies',
			successProperty: 'success'
		},
		writer: {
			type: 'json',
			successProperty: 'success'
		},
		listeners: {
			exception: function(proxy, res, oper, opts) {
				switch (oper.action) {
					case 'read':
						if (oper.success == false) {
							Ext.Msg.alert('Error', 'Failed to get a list of '+
							'currencies. Server returned an error.');
						}
						break;
					case 'create':
						if (oper.success == false) {
							var store = oper.records[0].store;
							store.remove(oper.records);
							Ext.Msg.alert('Error', 'Failed to create new '+
							'currency. Server returned an error.');
						}
						break;
					case 'update':
						if (oper.success == false) {
							oper.records[0].set(oper.records[0].raw);
							Ext.Msg.alert('Error', 'Failed to update currency.'+
							' Server returned an error.');
						}
						break;
					case 'destroy':
						if (oper.success == false) {
							Ext.Msg.alert('Error', 'Failed to delete currency.'+
							' Server returned an error.');
						}
						break;
					default:
						Ext.Msg.alert('Error', 'An unexpected error occured '+
						'while performing '+oper.action+' operation. Server '+
						'is saying: '+res.responseText);
				}//switch
			}//exception
		}//listeners
	},//proxy

	/*
		Returns a value formatted according to the currency fields.
	*/
	formatValue: function(value, record) {
		var suffix = record.get('suffix') ? record.get('suffix') : '';
		Ext.util.Format.currencyPrecision  = record.get('precision');
		Ext.util.Format.currencySign       = 
			record.get('prefix') ? record.get('prefix') : '';
		Ext.util.Format.decimalSeparator   = record.get('decimal');
		Ext.util.Format.thousandSeparator  = record.get('thousands');
		return Ext.util.Format.currency(value) + suffix;
	},//formatValue
	
	/*
		Returns an example value formatted according to the currency fields.
	*/
	formatExample: function(record) {
		var suffix = record.get('suffix') ? record.get('suffix') : '';
		Ext.util.Format.currencyPrecision  = record.get('precision');
		Ext.util.Format.currencySign       = 
			record.get('prefix') ? record.get('prefix') : '';
		Ext.util.Format.decimalSeparator   = record.get('decimal');
		Ext.util.Format.thousandSeparator  = record.get('thousands');
		return Ext.util.Format.currency('12345.67') + suffix;
	}//formatExample
});